package com.example.airport.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.airport.net.G;
import com.example.airport.parent.ICallback;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MyService extends Service {

    MqttClient mqttClient;
    boolean flag=false;
    ICallback callback;
    public class MyBinder extends Binder {

        public MyService getService(){
            return MyService.this;
        }
    }
    private MyBinder binder = new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void getMqtt(ICallback callback,String brokerUrl,String clientId,String[] topic,int qos[],String userName,String password)
    {
            new Thread(() -> {
                try {
                mqttClient = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setCleanSession(true);
                connOpts.setUserName(userName);
                connOpts.setPassword(password.toCharArray());

                mqttClient.connect(connOpts);

                try {
                    mqttClient.subscribe(topic, qos);
                } catch (MqttException e) {
                    e.printStackTrace();
                }


                mqttClient.setCallback(new MqttCallback() {
                    @Override
                    public void connectionLost(Throwable cause) {
                        try {
                            mqttClient.connect(connOpts);
                            callback.onSubscribedData(1,"","自动重连...");
                        }catch (Exception e)
                        {

                        }

                    }

                    @Override
                    public void messageArrived(String topic, MqttMessage message) throws Exception {

//                        System.out.println("======收到===="+topic);
//                        System.out.println(message.toString());
//                        System.out.println("======收到===="+topic);
                        // 处理接收到的消息
                       callback.onSubscribedData(0,topic,message.toString());
                    }

                    @Override
                    public void deliveryComplete(IMqttDeliveryToken token) {
                    }
                });

                    callback.onSubscribedData(1,"","连接成功!");
                } catch (MqttException e) {
                      callback.onSubscribedData(1,"",e.toString());
                }


            }).start();


    }


    public void toPublish(String topic,String text)  {
        try {

            MqttMessage message = new MqttMessage(text.getBytes());
            mqttClient.publish(topic,message);
        }catch (Exception e)
        {
            System.out.println("=发布时间报错==="+e.toString());
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("henry","执行了onStartCommand()");
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            mqttClient.disconnect();
        }catch (Exception e)
        {

        }
        Log.d("henry","执行了onDestory()");
    }


}
